//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "BrownianGeneratorFactory.h"
using namespace Cephei::QL::Models::Marketmodels;
#include <gen/QL/Models/Marketmodels/BrownianGenerator.h>
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::CBrownianGeneratorFactory::CBrownianGeneratorFactory (boost::shared_ptr<QuantLib::BrownianGeneratorFactory>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBrownianGeneratorFactory = NULL;
#endif
	_ppBrownianGeneratorFactory = &childNative;
    
}
Cephei::QL::Models::Marketmodels::CBrownianGeneratorFactory::CBrownianGeneratorFactory (QuantLib::BrownianGeneratorFactory& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phBrownianGeneratorFactory = NULL;
#endif
	_ppBrownianGeneratorFactory = new boost::shared_ptr<QuantLib::BrownianGeneratorFactory> (&childNative);
    
    _BrownianGeneratorFactoryOwner = owner;
    
}

Cephei::QL::Models::Marketmodels::CBrownianGeneratorFactory::CBrownianGeneratorFactory (CBrownianGeneratorFactory^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBrownianGeneratorFactory = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBrownianGeneratorFactory = new boost::shared_ptr<QuantLib::BrownianGeneratorFactory> (copy->GetShared());
        
    }
}
Cephei::QL::Models::Marketmodels::CBrownianGeneratorFactory::CBrownianGeneratorFactory (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBrownianGeneratorFactory = NULL;
#endif
	if (!t->IsSubclassOf(CBrownianGeneratorFactory::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::CBrownianGeneratorFactory::CBrownianGeneratorFactory (QuantLib::Handle<QuantLib::BrownianGeneratorFactory>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phBrownianGeneratorFactory = &childNative;
	_ppBrownianGeneratorFactory = &static_cast<boost::shared_ptr<QuantLib::BrownianGeneratorFactory>>(childNative.currentLink());
    
    _BrownianGeneratorFactoryOwner = owner;
}
Cephei::QL::Models::Marketmodels::CBrownianGeneratorFactory::CBrownianGeneratorFactory (QuantLib::Handle<QuantLib::BrownianGeneratorFactory> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phBrownianGeneratorFactory = &childNative;
	_ppBrownianGeneratorFactory = &static_cast<boost::shared_ptr<QuantLib::BrownianGeneratorFactory>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::CBrownianGeneratorFactory::CBrownianGeneratorFactory (QuantLib::BrownianGeneratorFactory childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBrownianGeneratorFactory = NULL;
#endif
	_ppBrownianGeneratorFactory = new boost::shared_ptr<QuantLib::BrownianGeneratorFactory> (new QuantLib::BrownianGeneratorFactory (childNative));
    
}
#endif

Cephei::QL::Models::Marketmodels::CBrownianGeneratorFactory::~CBrownianGeneratorFactory ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppBrownianGeneratorFactory != NULL)
    {
	    delete _ppBrownianGeneratorFactory;
        _ppBrownianGeneratorFactory = NULL;
    }
}
Cephei::QL::Models::Marketmodels::CBrownianGeneratorFactory::!CBrownianGeneratorFactory ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppBrownianGeneratorFactory != NULL)
    {
	    delete _ppBrownianGeneratorFactory;
    }
}
QuantLib::BrownianGeneratorFactory& Cephei::QL::Models::Marketmodels::CBrownianGeneratorFactory::GetReference ()
{
    if (_ppBrownianGeneratorFactory == NULL) throw gcnew NativeNullException ();
	return **_ppBrownianGeneratorFactory;
}
boost::shared_ptr<QuantLib::BrownianGeneratorFactory>& Cephei::QL::Models::Marketmodels::CBrownianGeneratorFactory::GetShared ()
{
    if (_ppBrownianGeneratorFactory == NULL) throw gcnew NativeNullException ();
	return *_ppBrownianGeneratorFactory;
}
QuantLib::BrownianGeneratorFactory* Cephei::QL::Models::Marketmodels::CBrownianGeneratorFactory::GetPointer ()
{
    if (_ppBrownianGeneratorFactory == NULL) throw gcnew NativeNullException ();
	return &**_ppBrownianGeneratorFactory;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BrownianGeneratorFactory>& Cephei::QL::Models::Marketmodels::CBrownianGeneratorFactory::GetHandle ()
{
	if (_phBrownianGeneratorFactory == NULL)
	{
		_phBrownianGeneratorFactory = new Handle<QuantLib::BrownianGeneratorFactory> (*_ppBrownianGeneratorFactory);
	}
	return *_phBrownianGeneratorFactory;
}
#endif
bool Cephei::QL::Models::Marketmodels::CBrownianGeneratorFactory::HasNative () 
{
	return (_ppBrownianGeneratorFactory != NULL);
}

Cephei::QL::Models::Marketmodels::IBrownianGenerator^ Cephei::QL::Models::Marketmodels::CBrownianGeneratorFactory::Create (UInt64 factors, UInt64 steps)
{
    try
    {
        QuantLib::Size _factors = (QuantLib::Size)ValueHelper::Convert (factors);
        QuantLib::Size _steps = (QuantLib::Size)ValueHelper::Convert (steps);
    	boost::shared_ptr<QuantLib::BrownianGenerator> _rv = (boost::shared_ptr<QuantLib::BrownianGenerator>)(*_ppBrownianGeneratorFactory)->create ( _factors,  _steps );   
        Cephei::QL::Models::Marketmodels::CBrownianGenerator^ _nrv = gcnew Cephei::QL::Models::Marketmodels::CBrownianGenerator (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

